﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DXWindowsApplication1.MainForm
{
    class Support
    {
        string _path = "";

        public int driveImage(DriveInfo drvinfo)
        {
            int imageindex;
            switch (drvinfo.DriveType)
            {
                case DriveType.Fixed:
                    {
                        imageindex = 3;
                        break;
                    }
                case DriveType.CDRom:
                    {
                        imageindex = 4;
                        break;
                    }
                case DriveType.Removable:
                    {
                        imageindex = 5;
                        break;
                    }
                default:
                    {
                        imageindex = 3;
                        break;
                    }
            }
            return imageindex;
        }

        #region Get path of directories

        public string getPath(TreeNode node)
        {
            string path = "";
            string[] namelist = node.FullPath.Split('\\');
            string nodename = namelist.GetValue(1).ToString();

            switch (nodename)
            {
                case "My Computer":
                    {
                        path = "MyComputer";
                        break;
                    }
                default:
                    {
                        path = "";
                        break;
                    }
            }

            return path;
        }

        public string getPathDirectories(TreeNode node)
        {
            string path = "";
            string[] namelist = node.FullPath.Split('\\');

            for (int i = 1; i < namelist.Length; i++)
            {
                path += namelist[i] + "\\";
            }

            return path;
        }

        public string getRootPathDirectories(TreeNode node)
        {
            string path = "";
            string[] namelist = node.FullPath.Split('\\');

            for (int i = 1; i < namelist.Length - 1; i++)
            {
                path += namelist[i] + "\\";
            }

            return path;
        }

        #endregion

        #region Create item on listview
        public ListViewItem CreateDriveListViewItem(DriveInfo driveinfo) {

            string driveName = "", driveType = "", totalSpace = "", freeSpace = "";

            if (driveinfo.DriveType == DriveType.Fixed)
            {

                double totalspace = Math.Round((double)driveinfo.TotalSize / Math.Pow(1024, 3), 2);
                double freespace = Math.Round((double)driveinfo.AvailableFreeSpace / Math.Pow(1024, 3), 2);
                totalSpace = totalspace.ToString();
                freeSpace = freespace.ToString();

                if (driveinfo.VolumeLabel != null)
                {
                    driveName = driveinfo.VolumeLabel + " " + driveinfo.Name;
                }
            }
            else {
                driveName = driveinfo.Name;
            }

            driveType = driveinfo.DriveType.ToString();
            string[] a = {driveName, driveType, totalSpace,freeSpace,driveinfo.Name};
            ListViewItem item = new ListViewItem(a);
            item.ImageIndex = 3;//driveImage(driveinfo);
            return item;
        }

        public ListViewItem CreateFolderListViewItem(DirectoryInfo dirinfo) {
            ListViewItem item ;

            string[] info = { dirinfo.Name,"", "Folder", dirinfo.LastWriteTime.ToShortTimeString(), dirinfo.FullName, dirinfo.Parent.FullName };
            item = new ListViewItem(info);
            item.ImageIndex = 0;
            return item;
        }

        public ListViewItem CreateFileListViewItem(FileInfo fileinfo) {
            ListViewItem item;
            string [] info = { fileinfo.Name, fileinfo.Extension, fileinfo.Length/1024 +" KB",
                                 fileinfo.LastWriteTime.ToShortTimeString(),fileinfo.FullName, fileinfo.Directory.FullName };
            item = new ListViewItem(info);
            #region "extension file"

            switch (fileinfo.Extension)
            {
                case ".txt":
                    {
                        item.ImageIndex = 11;
                        break;
                    }
                case ".pdf":
                    {
                        item.ImageIndex = 15;
                        break;
                    }
                case ".exe":
                    {
                        item.ImageIndex = 18;
                        break;
                    }
                case ".bmp":
                    {
                        item.ImageIndex = 8;
                        break;
                    }
                case ".png":
                    {
                        item.ImageIndex = 19;
                        break;
                    }
                case ".jpg":
                case ".jpeg":
                    {
                        item.ImageIndex = 20;
                        break;
                    }
                case ".gif":
                    {
                        item.ImageIndex = 21;
                        break;
                    }
                case ".avi":
                case ".mkv":
                case ".wmv":
                case ".flv":
                case ".mp3":
                case ".wav":
                case ".wma":
                    {
                        item.ImageIndex = 22;
                        break;
                    }
                case ".doc":
                    {
                        item.ImageIndex = 9;
                        break;
                    }
                case ".xls":
                    {
                        item.ImageIndex = 13;
                        break;
                    }
                case ".ppt":
                    {
                        item.ImageIndex = 23;
                        break;
                    }
                case ".rar":
                case ".zip":
                    {
                        item.ImageIndex = 24;
                        break;
                    }
                case ".html":
                case ".htm":
                    {
                        item.ImageIndex = 25;
                        break;
                    }
                default:
                    {
                        item.ImageIndex = 6;
                        break;
                    }
            }

            #endregion
            return item ;
        }
        #endregion

        public string GetDrivesItem(ListView listview)
        {
            listview.Clear();

            try
            {
                foreach (DriveInfo driveinfo in DriveInfo.GetDrives())
                {
                    ListViewItem item = CreateDriveListViewItem(driveinfo);
                    /*
                    item.SubItems.Add(driveinfo.Name);
                    item.SubItems.Add(Convert.ToString(driveinfo.AvailableFreeSpace));
                    item.SubItems.Add(driveinfo.DriveType.ToString());
                    item.SubItems.Add("");
                    */
                    listview.Items.Add(item);
                }

                _path = "My Computer";

                return _path;
            }
            catch
            {
                MessageBox.Show("Truy cập bị từ chối", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }
        }

        public string GetDirectoriesItem(DirectoryInfo dirinfo, ListView listview)
        {
            listview.Clear();

            ListViewItem item;

            try
            {
                foreach (DirectoryInfo directoryinfo in dirinfo.GetDirectories())
                {
                    item = CreateFolderListViewItem(directoryinfo);

                    item.SubItems.Add(directoryinfo.Name);
                    item.SubItems.Add("");
                    item.SubItems.Add(directoryinfo.Extension);
                    item.SubItems.Add(directoryinfo.LastWriteTime.ToShortDateString());

                    listview.Items.Add(item);
                }

                foreach (FileInfo fileinfo in dirinfo.GetFiles())
                {
                    item = CreateFileListViewItem(fileinfo);

                    item.SubItems.Add(fileinfo.Name);
                    item.SubItems.Add(Convert.ToString(fileinfo.Length / 1024));
                    item.SubItems.Add(fileinfo.Extension);
                    item.SubItems.Add(fileinfo.LastWriteTime.ToShortDateString());

                    listview.Items.Add(item);
                }

                _path = dirinfo.FullName;

                return _path;
            }
            catch
            {
                MessageBox.Show("Truy cập bị từ chối", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }
        }

        #region Get item in folder on treeview

        //public string GetDriveItem(ListView listview) {

        //    listview.Clear();
        //    foreach (DriveInfo driveinfo in DriveInfo.GetDrives()) {
        //        ListViewItem driveitem = CreateDriveListViewItem(driveinfo);
        //        listview.Items.Add(driveitem);
        //    }
        //    return _path = "My Computer";
        //}

        //public string GetDirectoryItem(DirectoryInfo dirinfo, ListView listview) {

        //    listview.Clear();
        //    ListViewItem item;
        //    foreach (DirectoryInfo diritem in dirinfo.GetDirectories()) {
        //        item = CreateFolderListViewItem(diritem);
        //        item.SubItems.Add(diritem.Name);
        //        item.SubItems.Add(diritem.Extension);
        //        item.SubItems.Add(diritem.LastWriteTime.ToShortTimeString());
        //        listview.Items.Add(item);
        //    }
        //    foreach (FileInfo fileitem in dirinfo.GetFiles())
        //    {
        //        item = CreateFileListViewItem(fileitem);
        //        item.SubItems.Add(fileitem.Name);
        //        item.SubItems.Add(fileitem.Extension);
        //        item.SubItems.Add(fileitem.LastWriteTime.ToShortTimeString());
        //        item.SubItems.Add(Convert.ToString(fileitem.Length / 1024));
        //        listview.Items.Add(item);
        //    }

        //    return _path = dirinfo.FullName;
        //}
        #endregion
    }
}
